//
//  LoginViewController.m
//  NiuNiuPacking
//
//  Created by shaoting on 14/10/20.
//  Copyright (c) 2014年 Reseek. All rights reserved.
//
#import "LoginViewController.h"
#import "Toast+UIView.h"
#import "NSString+SBJSON.h"
#import "ASIFormDataRequest.h"


@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{}];
}

- (IBAction)btnLogin:(id)sender {
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    //请求url
    NSURL *url = [NSURL URLWithString:loginURL];
    
    //创建请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [request setHTTPMethod:@"POST"];//设置请求方式POST
    NSString *str = [NSString stringWithFormat:@"&phone=%@&password=%@&platform=2",self.txtPhone.text,self.txtPassword.text];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    
    //连接服务器
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSString *responseStr = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [responseStr JSONValue];
    
    int errnum = [jsonDict[@"errno"] intValue];
    if(errnum == 0) {
        self.userBl = [[UserBl alloc] init];
        self.user = [[User alloc] init];
        self.user.id = 1;
        self.user.carNumber = @"111111";//[self getCarNumber];
        self.user.phone = self.txtPhone.text;
        self.user.password = self.txtPassword.text;
        self.user.token = jsonDict[@"token"];
        [self.userBl add:self.user];
        delegate.currentUser=self.user;
        [self dismissViewControllerAnimated:YES completion:^{NSLog(@"登录成功！");}];
    } else {
        [self.view makeToast:jsonDict[@"errmsg"]
                    duration:1.0
                    position:@"bottom"];

    }
}

-(NSString *)getCarNumber{
      AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSURL *url = [NSURL URLWithString:p_get_user_dataURL];
    
    //创建请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [request setHTTPMethod:@"POST"];//设置请求方式POST
    NSString *str = [NSString stringWithFormat:@"&token=%@",delegate.currentUser.token];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    
    //连接服务器
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSString *responseStr = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [responseStr JSONValue];
    
    int errnum = [jsonDict[@"errno"] intValue];
    if(errnum == 0) {
        return jsonDict[@"car_number"];
    }
    else{
        return @"";
    }
}



- (IBAction)btnReg:(id)sender {
    UIStoryboard *mainStoryboard =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *regViewControllor=[mainStoryboard instantiateViewControllerWithIdentifier:@"registerController"];
    regViewControllor.modalPresentationStyle=UIModalPresentationFormSheet;
    regViewControllor.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
    [self presentViewController:regViewControllor animated:YES completion:^{NSLog(@"跳出去注册页面。");
    }];
}
@end
